﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;
using CustomUIControls;


namespace SalesVMT
{
    public partial class frmProviderList : DockContent
    {
        int pageSize = UIParamater.PageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int provider_ID;
        
	    public int Provider_ID
	    {
		    get { return provider_ID;}
		    set { provider_ID = value;}
	    }
        private string provider_Name;

        public string Provider_Name
        {
            get { return provider_Name; }
            set { provider_Name = value; }
        }

        private bool selected;

        public frmProviderList(bool _selected)
        {
            InitializeComponent();
            btnDel.Enabled = btnEdit.Enabled = Program.isAdmin;
            DisplayFrame();
            LoadData();
            selected = _selected;
        }

        private void DisplayFrame()
        {
            DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
            dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
            dgvList1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
            dgvList1.AutoGenerateColumns = false;
            dgvList1.AllowUserToResizeRows = false;
            
            UISetting.AddColumn(ref dgvList1, "Provider_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", ColumnWidth.RowNumber, true, true);
            UISetting.AddColumn(ref dgvList1, "Provider_Code", "Mã NCC", "", ColumnWidth.Code3, true, true);
            UISetting.AddColumn(ref dgvList1, "Provider_Name", "Tên NCC", "", ColumnWidth.CustomerName, true, true);
            UISetting.AddColumn(ref dgvList1, "Provider_Address", "Địa chỉ", "", ColumnWidth.Address, true, true);
            UISetting.AddColumn(ref dgvList1, "Provider_TaxNo", "Mã số thuế", "", ColumnWidth.TaxCode, true, true);
            UISetting.AddColumn(ref dgvList1, "Provider_Phone", "Điện thoại", "", ColumnWidth.Phone, true, true);
            UISetting.AddColumn(ref dgvList1, "Provider_Contact", "Liên hệ", "", ColumnWidth.Contact, true, true);
        }
        private void LoadData()
        {
            DataTable dt = dt = ProviderDB.GetList(out itemCount, UISetting.GetKeyWord(txtKeyWord.Text), currPage, pageSize).Tables[0];
            PageIndex();
            dgvList1.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvList1.Rows.Insert(i, 1);
                dgvList1.Rows[i].Cells["Provider_ID"].Value = dt.Rows[i]["Provider_ID"].ToString();
                dgvList1.Rows[i].Cells["Provider_Code"].Value = dt.Rows[i]["Provider_Code"].ToString();
                dgvList1.Rows[i].Cells["Provider_Name"].Value = dt.Rows[i]["Provider_Name"].ToString();
                dgvList1.Rows[i].Cells["Provider_Address"].Value = dt.Rows[i]["Provider_Address"].ToString();
                dgvList1.Rows[i].Cells["Provider_TaxNo"].Value = dt.Rows[i]["Provider_TaxNo"].ToString();
                dgvList1.Rows[i].Cells["Provider_Phone"].Value = dt.Rows[i]["Provider_Phone"].ToString();
                dgvList1.Rows[i].Cells["Provider_Contact"].Value = dt.Rows[i]["Provider_Contact"].ToString();
                dgvList1.Rows[i].Cells["RowNumber"].Value = dt.Rows[i]["RowNumber"].ToString();
                //dgvList1.Rows[i].Cells["Provider_Remaining"].Value = Convert.ToDecimal(dt.Rows[i]["Provider_Remaining"]).ToString("#,##0");
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
               provider_ID = Convert.ToInt32(dgvList1["Provider_ID", dgvList1.SelectedCells[0].RowIndex].Value);
               if (ProviderDB.Using(provider_ID) > 0)
               {
                   MessageBox.Show("NCC này đang sử dụng! Không xóa được.", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                   return;
               }
                if (MessageBox.Show(Message.Delete_Question, Message.Info, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (ProviderDB.Delete(provider_ID))
                    {
                        Program.taskbarNotifier.Show(Message.Info, Message.Delete_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(Message.Delete_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
                provider_ID = Convert.ToInt32(dgvList1["Provider_ID", dgvList1.SelectedCells[0].RowIndex].Value);
                Form f = new frmProvider(provider_ID);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Form f = new frmProvider(0);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void dgvList1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

                if (e.RowIndex >= 0)
                {
                    if (selected)
                    {
                        provider_ID = Convert.ToInt32(dgvList1["Provider_ID", dgvList1.SelectedCells[0].RowIndex].Value);
                        provider_Name = dgvList1["Provider_Name", dgvList1.SelectedCells[0].RowIndex].Value.ToString();
                        //provider_Remaining = Convert.ToDecimal(dgvList1["Provider_Remaining", dgvList1.SelectedCells[0].RowIndex].Value);
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                        if (Program.isAdmin)
                        {
                        btnEdit_Click(null, null);
                        }
                }
        }
    }
}